package com.example.shop_2211.login

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2211.R
import com.example.shop_2211.databinding.ActivityUpdatePwdBinding
@Route(path="/app/UpdatePwdActivity")
class UpdatePwdActivity : BaseActivity<ActivityUpdatePwdBinding,LoginViewModel>() {
    override fun initView() {
        val phone = SPUtils.getInstance().getString("phone")
        binding.tvPhone.text =phone //先显示手机号
        //点击获取验证码
        binding.btnGetCode.setOnClickListener {
            viewModel.sendCode(phone)
        }

        binding.btnUpdate.setOnClickListener {
            val pwd = binding.etPwd.text.toString()
            if(pwd.isNullOrEmpty()){
                ToastUtils.showLong("请输入密码")
                return@setOnClickListener
            }
            viewModel.updatePwd(phone,binding.etCode.text.toString(),pwd)
        }

    }

    override fun initData() {
        viewModel.sendCodeSuccess.observe(this){
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.sendCodeFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.updatePwdSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                finish()
            }
        }
        viewModel.updatePwdFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_update_pwd
    }

}